package com.trdcore.zyzkt.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trdcore.zyzkt.listener.SubjectListener;
import com.trdcore.zyzkt.mapper.SubjectMapper;
import com.trdcore.zyzkt.model.vod.Subject;
import com.trdcore.zyzkt.service.SubjectService;
import com.trdcore.zyzkt.vo.vod.SubjectEeVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author Luffy
 * @since 2024-03-17
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    private SubjectListener subjectListener;
    @Override
    public List<Subject> selectSubjectList(Long id) {

        QueryWrapper<Subject> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Subject> subjectList = baseMapper.selectList(wrapper);
        for (Subject subject : subjectList) {
            Long subjectId = subject.getId();
            boolean isChild=this.isChild(subjectId);
            subject.setHasChildren(isChild);
        }
        return subjectList;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName= URLEncoder.encode("课堂分类","UTF-8");
            response.setHeader("Content-disposition","attachment;filename*=utf-8"+fileName+".xlsx");
            List<Subject> dictList=baseMapper.selectList(null);
            List<SubjectEeVo> dictVoList=new ArrayList<>(dictList.size());
            for (Subject dict : dictList) {
                SubjectEeVo dictVo=new SubjectEeVo();
                BeanUtils.copyProperties(dict,dictVo);
                dictVoList.add(dictVo);
            }
            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class).sheet("课程分类").doWrite(dictVoList);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),
                    SubjectEeVo.class,subjectListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean isChild(Long subjectId) {
        QueryWrapper<Subject> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",subjectId);
        Long count = baseMapper.selectCount(wrapper);
        return count>0;
    }
}
